package com.njpi.stu.utils;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.util.HashMap;
//发送手机短信消息的工具类
@Component
@PropertySource({"classpath:user.properties"})
public class SendSms {


    @Value("${SignName}")
    private String SignName;
    @Value("${TemplateCode}")
    private String TemplateCode;

    @Value("${accessKeyId}")
    private String accessKeyId;

    @Value("${accessSecret}")
    private String accessSecret;


    public  void sendMsm(HashMap<String,String> params){
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", params.get("PhoneNumbers"));//电话号码
        request.putQueryParameter("SignName", SignName);//配置文件读取
        request.putQueryParameter("TemplateCode", TemplateCode);//配置文件读取
        request.putQueryParameter("TemplateParam", params.get("TemplateParam"));//json格式，{"code"："1234"}
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }

    }

    /*public static void main(String[] args) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou","LTAI4FxULxXbAAjiB3PWCx84","IsRtHpF4W6DSNsgeUJGRTFu1VWUvGn");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", "18051983695");
        request.putQueryParameter("SignName", "猫咪老师");
        request.putQueryParameter("TemplateCode", "SMS_174580777");
        request.putQueryParameter("TemplateParam", "{\"code\":\"23456\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }*/
    //}
}